using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SharpWind;
using SharpWind.States;
using Microsoft.DirectX;

using SharpWind.Graphics;
using SharpWind.Input;
using Microsoft.DirectX.Direct3D;
using SharpWind.Scene;
using SharpWind.Math;
using System.Drawing;
using SharpWind.Helpers;

namespace Game
{
	public class TestState : State
	{
		StaticObject box = null;

		Camera camera = null;

		float time = 0f;

		public TestState() : base( "TestState" )
		{

		}

		public override void Load()
		{
			this.box = new StaticObject( "box.x", "rockwall.dds", "rockwall_normal.dds" );
			this.box.PositionX = -5f;

			this.camera				= new FirstPersonCamera();
			this.camera.Position	= new Vector3( 0, 8, -10 );
			this.camera.LookAt		= new Vector3( 0, 0, 5 );
			this.camera.Activate();

			Engine.Instance.Scene.LightPosition = new Vector3( 0, 8, -15 );

			Engine.Instance.Scene.AddObject( "box", this.box );
			Engine.Instance.Scene.AddCamera( "player", this.camera );

			Engine.Instance.Keyboard.KeyDown += new KeyboardEventHandler( Keyboard_KeyDown );
		}

		private void Keyboard_KeyDown( Keyboard keyboard, KeyboardEventArgs args )
		{
			FirstPersonCamera camera = Engine.Instance.Scene.ActiveCamera as FirstPersonCamera;

			float speed = 20f;

			if( camera != null )
			{
				switch( args.KeyCode )
				{
					case Key.W:
						camera.Move( Direction.Forward, speed * time );
						break;

					case Key.S:
						camera.Move( Direction.Forward, -speed * time );
						break;

					case Key.A:
						camera.Strafe( -speed * time );
						break;

					case Key.D:
						camera.Strafe( speed * time );
						break;

					case Key.D1:
						Engine.Instance.PostProcessing.Shader = PostProcessingEffects.Normal;
						break;

					case Key.D2:
						Engine.Instance.PostProcessing.Shader = PostProcessingEffects.Invert;
						break;
				}
			}
		}

		public override void Update( float elapsedTime )
		{
			this.time = elapsedTime;

			// Create the light
			//Engine.Instance.Graphics.Device.Lights[0].Type = LightType.Directional;
			//Engine.Instance.Graphics.Device.Lights[0].Diffuse = Color.White;
			//Engine.Instance.Graphics.Device.Lights[0].Direction = Vector3.Normalize( new Vector3( -1.0f, -1.0f, 1.0f ) );
			//Engine.Instance.Graphics.Device.Lights[0].Update();
			//Engine.Instance.Graphics.Device.Lights[0].Enabled = true;


			// Set render states
			FirstPersonCamera camera = Engine.Instance.Scene.ActiveCamera as FirstPersonCamera;
			float speed = 20f;

			if( Engine.Instance.Keyboard.IsKeyDown( Key.D1 ) )
			{
				Engine.Instance.PostProcessing.Shader = PostProcessingEffects.Normal;
			}

			if( Engine.Instance.Keyboard.IsKeyDown( Key.D2 ) )
			{
				Engine.Instance.PostProcessing.Shader = PostProcessingEffects.Invert;
			}

			if( Engine.Instance.Keyboard.IsKeyDown( Key.W) )
			{
				camera.Move( Direction.Forward, speed * time );
			}

			if( Engine.Instance.Keyboard.IsKeyDown( Key.A ) )
			{
				camera.Strafe( -speed * time );
			}
		}

		public override void Render()
		{
			
		}
	}

	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault( false );

			Engine.Instance.Initialized += delegate
			{
				Engine.Instance.States.Add( new TestState() );
			};

			Settings settings = Settings.Default;
			settings.Scale = 0.01f;

			if( Engine.Instance.Initialize( settings ) )
			{
				Engine.Instance.Run();
			}
		}
	}
}